#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"

robots_txt_build_config() {
  local APP="$1"; verify_app_name "$APP"
  local NGINX_TEMPLATE="$PLUGIN_AVAILABLE_PATH/robots.txt/templates/robots-txt.conf.sigil"
  local ROBOTS_TXT_TEMPLATE="$PLUGIN_AVAILABLE_PATH/robots.txt/templates/robots.txt.sigil"
  local TMP_WORK_DIR=$(mktemp -d /tmp/dokku_robots.txt.XXXXX)
  local NGINX_CONF=$(mktemp --tmpdir="${TMP_WORK_DIR}" "robots-txt.conf.XXXXXX")
  local ROBOTS_TXT=$(mktemp --tmpdir="${TMP_WORK_DIR}" "robots.txt.XXXXXX")

  # Setup
  mkdir -p "$DOKKU_ROOT/$APP/nginx.conf.d/"
  chown dokku:dokku "$DOKKU_ROOT/$APP/nginx.conf.d/"

  # Create Nginx conf
  local NGINX_SIGIL_PARAMS=(-f $NGINX_TEMPLATE APP="$APP" DOKKU_ROOT="$DOKKU_ROOT")
  dokku_log_info1 "Creating robots.txt Nginx conf"
  sigil "${NGINX_SIGIL_PARAMS[@]}" | cat -s > "$NGINX_CONF"
  cp "$NGINX_CONF" "$DOKKU_ROOT/$APP/nginx.conf.d/robots-txt.conf"

  # Create robots.txt
  local ROBOTS_TXT_SIGIL_PARAMS=(-f $ROBOTS_TXT_TEMPLATE APP="$APP" DOKKU_ROOT="$DOKKU_ROOT" ROBOTS_TXT_DISALLOW="$2")
  dokku_log_info1 "Creating robots.txt"
  sigil "${ROBOTS_TXT_SIGIL_PARAMS[@]}" | cat -s > "$ROBOTS_TXT"
  cp "$ROBOTS_TXT" "$DOKKU_ROOT/$APP/robots.txt"

  # Set permissions
  chmod +r "$DOKKU_ROOT/$APP/nginx.conf.d/robots-txt.conf"
  chmod +r "$DOKKU_ROOT/$APP/robots.txt"

  if (is_deployed "$APP"); then
    dokku_log_verbose "Reloading nginx"
    validate_nginx && restart_nginx
  fi

  dokku_log_info1 "Finished"
}
